package com.itheima.pay.controller;

import com.github.wxpay.sdk.WXPay;
import com.itheima.pay.config.WXPayConfigCustom;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("wxpay")
public class WxPayController {



    /**
     * 预下单：返回二维码的链接，二维码链接生成二维码图片
     * https://openapi.alipay.com/gateway.do   生产环境地址
     * https://openapi.alipaydev.com/gateway.do  沙箱环境地址
     */
    @GetMapping("precreate/{orderNo}")
    public  Map<String, String> precreate(@PathVariable String orderNo) throws Exception {

        // 初始配置
        WXPayConfigCustom config = new WXPayConfigCustom();
        WXPay wxPay = new WXPay(config);

        //封装参数
        Map<String, String> reqData = new HashMap<>();
        reqData.put("body", "88级大砍刀");
        reqData.put("out_trade_no", orderNo);
        reqData.put("total_fee", "1"); //订单总金额，单位为分
        reqData.put("spbill_create_ip", "192.168.11.11");
        reqData.put("notify_url", "http://www.itheima.com/notify_url"); // 回调地址
        reqData.put("trade_type", "NATIVE");

        // 执行
        Map<String, String> map = wxPay.unifiedOrder(reqData);
        return map;
    }


    /**
     * 查询支付结果
     */
    @GetMapping("query/{orderNo}")
    public Map<String, String> queryOrder(@PathVariable String orderNo) throws Exception {

        // 初始配置
        WXPayConfigCustom config = new WXPayConfigCustom();
        WXPay wxPay = new WXPay(config);

        //封装参数
        Map<String, String> reqData = new HashMap<>();
        reqData.put("out_trade_no", orderNo);
        // 执行
        Map<String, String> map = wxPay.orderQuery(reqData);
        return map;

    }

    /**
     * 退款
     */
    @GetMapping("refund/{orderNo}/{money}/{refundNo}")
    public Map<String, String> refund(@PathVariable String orderNo ,@PathVariable String money,@PathVariable String refundNo)  throws  Exception{

        // 初始配置
        WXPayConfigCustom config = new WXPayConfigCustom();
        WXPay wxPay = new WXPay(config);

        //封装参数
        Map<String, String> reqData = new HashMap<>();
        reqData.put("out_trade_no", orderNo);
        reqData.put("out_refund_no", refundNo);
        reqData.put("total_fee",  "1");  //订单金额
        reqData.put("refund_fee", money);
        reqData.put("refund_desc", "刀真假");
        reqData.put("notify_url", "http://www.itheima.com/notify_url");  // 回调通知
        // 执行
        Map<String, String> map = wxPay.refund(reqData);
        return map;

    }



    /**
     * 退款查询
     */
    @GetMapping("refund/{refundNo}")
    public Map<String, String> queryrefund(@PathVariable String refundNo)  throws  Exception{

        // 初始配置
        WXPayConfigCustom config = new WXPayConfigCustom();
        WXPay wxPay = new WXPay(config);

        //封装参数
        Map<String, String> reqData = new HashMap<>();
        reqData.put("out_refund_no", refundNo);
        // 执行
        Map<String, String> map = wxPay.refund(reqData);
        return map;

    }



}
